package com.adk.backend.controller;

import com.adk.backend.annotation.RequiresPermission;
import com.adk.backend.common.Result;
import com.adk.backend.entity.LxOrderTracking;
import com.adk.backend.service.OrderTrackingService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

/**
 * 订单跟踪管理控制器
 */
@Slf4j
@RestController
@RequestMapping("/api/order-tracking")
public class OrderTrackingController {
    
    @Autowired
    private OrderTrackingService orderTrackingService;
    
    /**
     * 根据订单ID查询跟踪记录列表
     */
    @RequiresPermission("view")
    @GetMapping("/list")
    public Result<List<Map<String, Object>>> getTrackingList(@RequestParam Long orderId) {
        List<Map<String, Object>> result = orderTrackingService.getTrackingListByOrderId(orderId);
        return Result.success(result);
    }
    
    /**
     * 根据ID查询跟踪记录
     */
    @RequiresPermission("view")
    @GetMapping("/detail")
    public Result<LxOrderTracking> getTrackingDetail(@RequestParam Long id) {
        LxOrderTracking tracking = orderTrackingService.getTrackingById(id);
        if (tracking != null) {
            return Result.success(tracking);
        }
        return Result.error("跟踪记录不存在");
    }
    
    /**
     * 创建跟踪记录
     */
    @RequiresPermission("add")
    @PostMapping("/create")
    public Result<LxOrderTracking> createTracking(@RequestBody LxOrderTracking tracking) {
        try {
            LxOrderTracking result = orderTrackingService.createTracking(tracking);
            return Result.success("创建成功", result);
        } catch (Exception e) {
            log.error("创建跟踪记录失败", e);
            return Result.error("创建跟踪记录失败：" + e.getMessage());
        }
    }
    
    /**
     * 批量创建跟踪记录
     */
    @RequiresPermission("add")
    @PostMapping("/batch-create")
    public Result<Boolean> batchCreateTracking(@RequestBody List<LxOrderTracking> trackingList) {
        try {
            boolean success = orderTrackingService.batchCreateTracking(trackingList);
            if (success) {
                return Result.success("批量创建成功", true);
            }
            return Result.error("批量创建失败");
        } catch (Exception e) {
            log.error("批量创建跟踪记录失败", e);
            return Result.error("批量创建跟踪记录失败：" + e.getMessage());
        }
    }
    
    /**
     * 更新跟踪记录
     */
    @RequiresPermission("edit")
    @PostMapping("/update")
    public Result<Boolean> updateTracking(@RequestBody LxOrderTracking tracking) {
        try {
            boolean success = orderTrackingService.updateTracking(tracking);
            if (success) {
                return Result.success("更新成功", true);
            }
            return Result.error("更新失败");
        } catch (Exception e) {
            log.error("更新跟踪记录失败", e);
            return Result.error("更新跟踪记录失败：" + e.getMessage());
        }
    }
    
    /**
     * 删除跟踪记录
     */
    @RequiresPermission("del")
    @PostMapping("/delete")
    public Result<Boolean> deleteTracking(@RequestParam Long id) {
        try {
            boolean success = orderTrackingService.deleteTracking(id);
            if (success) {
                return Result.success("删除成功", true);
            }
            return Result.error("删除失败");
        } catch (Exception e) {
            log.error("删除跟踪记录失败", e);
            return Result.error("删除跟踪记录失败：" + e.getMessage());
        }
    }
}

